<?php


namespace app\api\controller;

use app\admin\model\course\Course;
use app\admin\model\course\CourseContent;
use app\admin\model\course\Section;
use app\admin\model\course\UserCourse;
use app\admin\model\course\UserSection;
use app\common\controller\Api;
use app\admin\model\course\Train;

/**
 * 课程接口
 */
class Coures extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['train','course'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //培训机构
    public function train()
    {
        //获取省市区
        $province = $this->request->request('province');
        $city = $this->request->request('city');
        $area = $this->request->request('area');
        $where = [];
        if($province) $where['province'] = $province;
        if($city) $where['city'] = $city;
        if($area) $where['area'] = $area;
        if(empty($where)){
            $this->error(__('Invalid parameters'));
        }
        $model = new Train();
        $this->success('返回成功', $model->where($where)->select());
    }

    //课程
    public function course()
    {
        $train_id = $this->request->request('train_id'); //获取培训机构
        if (!$train_id) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('返回成功', Course::all(['train_id' => $train_id]));
    }
    //课程内容
    public function coursecontent()
    {
        $course_id = $this->request->request('course_id'); //获取课程
        if (!$course_id) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('返回成功', CourseContent::all(['course_id' => $course_id]));
    }
    //课程章节
    public function sectionlist()
    {
        $coursecontent_id = $this->request->request('coursecontent_id'); //获取课程

        if (!$coursecontent_id) {
            $this->error(__('Invalid parameters'));
        }

        $list = Section::all(['coursecontent_id' => $coursecontent_id]);

        $data = [];

        foreach ($list as $v){
            $data[] = [
                'section_id' => $v->id,
                'section_name' => $v->name,
                'section_image' => $v->image,
                'study_time' => 0,
                'study_status' => '正在学习',
            ];
        }

        $this->success('返回成功', [
            'list' => $data,
            'coursecontent' => CourseContent::get($coursecontent_id)]);
    }
    //章节详情
    public function sectiondetails()
    {
        $user = $this->auth->getUser();
        $id = $this->request->request('id'); //获取章节信息
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $this->success('返回成功', [
            'sectioninfo' => Section::get($id,['coursecontent']),
            'usersection' => UserSection::get(['section_id' => $id, 'user_id' => $user->id])//用户购买课程章节信息
        ]);
    }
}